<?php

namespace app\user\controller;

use app\common\controller\User;
use think\Db;

class Withdraw extends User
{

    /**
     * @return mixed
     */
    public function index()
    {
        //是否存在提现信息：
        $card_id = Db::name('user_bank')->where(['uid' => $this->uid])->value('card_id');
        if (empty($card_id)) $this->error('您还没有绑卡，不可提现', url('user/safe/bank'));

        $account_money = 0;
        if ($this->uid > 0) {
            $account_money = Db::name('user_money')->where(['uid' => $this->uid])->value('account_money');
        }
        $this->assign('account_money', $account_money);

        $this->assign('min', config('withdraw_min'));
        $this->assign('rate', config('withdraw_fee'));

        $this->assign('way', model('UserWithdraw')->withdraw_type);

        return $this->fetch();
    }

    /**
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function log()
    {

        $list = model('UserWithdraw')
            ->where(['uid' => $this->uid])
            ->paginate(15, $this->isMobile() ? true : false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());

        return $this->fetch();
    }

    /**
     * 提现处理
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function handle()
    {

        if ($this->request->isPost()) {

            $params = $this->request->param();

            $result = \app\common\serve\User::withdraw([
                'uid' => $this->uid,
//                'type' => $params['type'],
                'passoword' => $params['password'],
                'money' => $params['money']
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], 'index');
            } else {
                $this->error($result['msg']);
            }

        }
        $this->redirect('/');
    }

}
